﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGuiMVC.BllBookingService;

namespace WebGuiMVC.Controllers
{

    /// <summary>
    /// The StudentController
    /// </summary>
    public class StudentController : Controller
    {

        /// <summary>
        /// The BookingServiceClient
        /// </summary>
        private BookingServiceClient _booking;

        /// <summary>
        /// Construct the StudentController
        /// </summary>
        public StudentController()
        {
            try
            {
                _booking = new BookingServiceClient();
            }
            catch (Exception ex)
            {
                TempData["alertMessage"] = ex.Message;
            }
        }

        /// <summary>
        /// Get the Index view
        /// </summary>
        /// <returns>The Index view</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Get all the bookings as json
        /// </summary>
        /// <returns>All the bookings as json</returns>
        public JsonResult GetBookings()
        {
            try
            {
                return Json(_booking.GetBookings().ToList(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                TempData["alertMessage"] = ex.Message;
            }
        }

        /// <summary>
        /// Get the NewBooking view
        /// </summary>
        /// <returns>The NewBooking view</returns>
        public ActionResult NewBooking()
        {
            return View();
        }

        /// <summary>
        /// Add a new booking
        /// </summary>
        /// <param name="booking">The booking to add</param>
        /// <returns>Redirect to the index if the booking is added.
        /// Otherwise it will return the view</returns>
        [HttpPost]
        public ActionResult NewBooking(BeBooking booking)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string message = _booking.SaveBooking(booking);
                    TempData["alertMessage"] = message;
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    TempData["alertMessage"] = ex.Message;
                }
            }
            return View(booking);
        }

        /// <summary>
        /// Edit a booking with the id
        /// </summary>
        /// <param name="id">The id of the booking</param>
        /// <returns>The EditBooking view</returns>
        public ActionResult EditBooking(int id)
        {
            try
            {
                BeBooking booking = _booking.GetBookingFromId(id);
                return View(booking);
            }
            catch (Exception ex)
            {
                TempData["alertMessage"] = ex.Message;
            }
        }

        /// <summary>
        /// Edit a booking when the form is submitted
        /// </summary>
        /// <param name="booking">The booking to edit</param>
        /// <returns>The EditBooking view</returns>
        [HttpPost]
        public ActionResult EditBooking(BeBooking booking)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _booking.EditBooking(booking);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    TempData["alertMessage"] = ex.Message;
                }
            }
            return View(booking);
        }

        /// <summary>
        /// Delete a booking with the id
        /// </summary>
        /// <param name="id">The id of the booking</param>
        /// <returns>Redirect to the Index view</returns>
        public ActionResult DeleteBooking(int id)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    BeBooking booking = _booking.GetBookingFromId(id);
                    _booking.DeleteBooking(booking, "elev");
                }
                catch (Exception ex)
                {
                    TempData["alertMessage"] = ex.Message;
                }
            }
            return RedirectToAction("Index");
        }
    }
}